const app = getApp()
const publicFun = require('/utils/public.js')
import { getClimateActionList } from '/utils/request'

// let leftHeight = 0, rightHeight = 0; //分别定义左右两边的高度
let refresh = 0;

Page({
  data: {
    interval: 5000,
    swiperList: [],
    leftHeight: 0,
    rightHeight: 0,
    leftList: [],
    rightList: [],
    pageindex: 1,
  },
  onLoad() { // 页面加载
    // my.navigateTo({url: '/pages/demo/demo'})
    my.getSystemInfo({
      success: (res) => {
        this.setData({
          systemInfo: res
        })
      }
    });
    this.getBannerList();
    if(app.globalData.secondCount){
      console.log('yes', this.data.location)
    }else{
      console.log('no',this.data.location)
      app.getSecondCount().then(() => {
        this.getClimateActionList(this.data.pageindex);
        
        // 获取用户信息
        if(!app.globalData.userData.ngotype){
          app.getUserData();
        }
      });
    }
  },
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0 // 这里是tabbar下标，每个tabbar页面都需要写一下。
      });
    }
  },
  getBannerList() {
    // 获取当前时间的时间戳
    const nowTimestamp = Date.now();
    // 设置目标时间：2025年2月21日0点，并获取其时间戳
    const targetDate = new Date(2025, 1, 21, 0, 0, 0);
    const targetTimestamp = targetDate.getTime();
    // console.log(nowTimestamp, targetTimestamp);
    let bannerOne = '/images/banner/banner.png'
    if(nowTimestamp < targetTimestamp){
      bannerOne = '/images/banner/banner0.png'
    }
    this.setData({
      swiperList: [
        { id: 0, url: bannerOne },
        { id: 1, url: '/images/banner/banner_rank.png' },
        // { id: 2, url: '/images/banner/banner_byo.png' }
      ]
    })
  },
  swiperBannerTap(e) {
    let data = e.currentTarget.dataset;
    if(app.globalData.userid){
      switch (data.id) {
        case 1:
          my.navigateTo({
            url: '/pages/rankList/rankList'
          })
          break;
        case 2:
          my.navigateTo({
            url: '/pages/upload/upload?actionid=5'
          })
          break;
        default:
          my.navigateTo({
            url: '/pages/cardList/cardList'
          })
      }
    }else{
      publicFun.jumpLogin();
    }
  },
  async getClimateActionList(pageindex) { 
    try{
      var params = {
        duserid: app.globalData.userid ? app.globalData.userid : 0,
        airesult: '',
        userid: 0,
        typeid: app.globalData.actiontypeid,
        isbangdan: 0,
        pagesize: app.globalData.pagesize,
        pageindex: pageindex
      }
      const res = await getClimateActionList(params)
      my.stopPullDownRefresh();
      my.hideLoading();
      if(res.S == 1){
        let listArr = res.L;
        if(listArr.length > 0){
          this.wallList(listArr)
          app.globalData.hc = res.HC;
        }else{
          publicFun.showToast("已经加载到底啦")
        }
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败');
      }
    }catch(e){
      publicFun.showToast('失败，请稍后再试')
    }
  },
  wallList(list) {
    var that = this;
    var allData = list;
    //定义两个临时的变量来记录左右两栏的高度，避免频繁调用setData方法
    var leftH = that.data.leftHeight;
    var rightH = that.data.rightHeight;
    var leftData = that.data.leftList;
    var rightData = that.data.rightList;
    for (let i = 0; i < allData.length; i++) {
      if (leftH == rightH || leftH < rightH) {//判断左右两侧当前的累计高度，来确定item应该放置在左边还是右边
        leftData.push(allData[i]);
        leftH += Number(allData[i].CoverHeight);
      } else {
        rightData.push(allData[i]);
        rightH += Number(allData[i].CoverHeight);
      }
    }
    that.setData({
      leftHight: leftH,
      rightHight: rightH,
      leftList: leftData,
      rightList: rightData
    })

    if(refresh == 1){
      publicFun.showToast("刷新成功");
    }
  },
  
  onPullDownRefresh() {
    // 页面被下拉
    my.showLoading();
    // my.startPullDownRefresh();
    refresh = 1; //开始刷新
    this.getClimateActionList(1);
    this.setData({
      leftHight: 0,
      rightHight: 0,
      leftList: [],
      rightList: []
    });
  },
  onReachBottom(e) { //上拉加载
    refresh = 0;
    const newPage = this.data.pageindex + 1;
    this.getClimateActionList(newPage);
    this.setData({pageindex: newPage});
  },
});
